package com.just4e;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class MainUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6723147857266016046L;
	private JPanel contentPane;
	private JLabel srcLabel;
	private JTextField srcTextField;
	private JButton srcButton;
	private JLabel targetLabel;
	private JTextField targetTextField;
	private JButton targetButton;
	private JButton convertButton;
	private JButton resetButton;
	private JLabel nameLabel;
	private JTextField nameTextField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainUI frame = new MainUI();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainUI() {
		initComponents();
	}

	private void initComponents() {
		setTitle("源代码->HTML");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 260);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getSrcLabel());
		contentPane.add(getSrcTextField());
		contentPane.add(getSrcButton());
		contentPane.add(getTargetLabel());
		contentPane.add(getTargetTextField());
		contentPane.add(getTargetButton());
		contentPane.add(getConvertButton());
		contentPane.add(getResetButton());
		contentPane.add(getNameLabel());
		contentPane.add(getNameTextField());
	}

	private JLabel getSrcLabel() {
		if (srcLabel == null) {
			srcLabel = new JLabel("源代码目录");
			srcLabel.setBounds(25, 25, 80, 20);
		}
		return srcLabel;
	}

	private JTextField getSrcTextField() {
		if (srcTextField == null) {
			srcTextField = new JTextField();
			srcTextField.setBounds(110, 25, 220, 20);
			srcTextField.setColumns(10);
		}
		return srcTextField;
	}

	private JButton getSrcButton() {
		if (srcButton == null) {
			srcButton = new JButton("...");
			srcButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String text = getDirPath("源文件夹");
					if (text == null) {
						text = "";
					}
					getSrcTextField().setText(text);
				}
			});
			srcButton.setBounds(335, 25, 50, 20);
		}
		return srcButton;
	}

	private JLabel getTargetLabel() {
		if (targetLabel == null) {
			targetLabel = new JLabel("输出目录");
			targetLabel.setBounds(25, 76, 80, 20);
		}
		return targetLabel;
	}

	private JTextField getTargetTextField() {
		if (targetTextField == null) {
			targetTextField = new JTextField();
			targetTextField.setBounds(110, 76, 220, 20);
			targetTextField.setColumns(10);
		}
		return targetTextField;
	}

	private JButton getTargetButton() {
		if (targetButton == null) {
			targetButton = new JButton("...");
			targetButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String text = getDirPath("输出文件夹");
					if (text == null) {
						text = "";
					}
					getTargetTextField().setText(text);
				}
			});
			targetButton.setBounds(335, 75, 50, 20);
		}
		return targetButton;
	}

	private JButton getConvertButton() {
		if (convertButton == null) {
			convertButton = new JButton("转换");
			convertButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String src = getSrcTextField().getText();
					String target = getTargetTextField().getText();
					String name = getNameTextField().getText();
					if (isEmpty(src) || isEmpty(target) || isEmpty(name)) {
						JOptionPane.showMessageDialog(convertButton,
								"全部都需要正确填写");
					}
					convertButton.setEnabled(false);
					try {
						new Convter(name).mirror(src, target);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
					JOptionPane.showMessageDialog(convertButton, "转化完毕");
					convertButton.setEnabled(true);
				}
			});
			convertButton.setBounds(86, 177, 93, 23);
		}
		return convertButton;
	}

	private boolean isEmpty(String s) {
		return s == null || s.trim().length() == 0;
	}

	private JButton getResetButton() {
		if (resetButton == null) {
			resetButton = new JButton("重置");
			resetButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					getSrcTextField().setText("");
					getTargetTextField().setText("");
				}
			});
			resetButton.setBounds(241, 177, 93, 23);
		}
		return resetButton;
	}

	private String getDirPath(String title) {
		JFileChooser chooser = new JFileChooser();
		chooser.setDialogTitle(title);
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		chooser.setMultiSelectionEnabled(false);
		int value = chooser.showOpenDialog(this);
		if (value == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();
			if (!file.exists() || !file.isDirectory()) {
				JOptionPane.showMessageDialog(chooser, "请选择一个文件夹");
				return getDirPath(title);
			} else {
				return file.getAbsolutePath();
			}
		}
		return null;
	}

	private JLabel getNameLabel() {
		if (nameLabel == null) {
			nameLabel = new JLabel("项目名称");
			nameLabel.setBounds(25, 127, 80, 20);
		}
		return nameLabel;
	}

	private JTextField getNameTextField() {
		if (nameTextField == null) {
			nameTextField = new JTextField();
			nameTextField.setBounds(110, 127, 275, 20);
			nameTextField.setColumns(10);
		}
		return nameTextField;
	}
}
